#include "sled/sled_class/sled_class.h"
#include "sled_scene/sled_scene.h"

#include "debug.h"
#define LOG_TAG "[SLED_SCENE]"
inline static void switch_action_handler(sled_scene_action_t *action)
{
    action->sled->SetMode(SLED_MODE_SWITCH);
    switch(action->switch_param.switch_io_state)
    {
        case SLED_SCENE_SWITCH_IO_STATE_OFF:
            action->sled->OFF();
        break;
        case SLED_SCENE_SWITCH_IO_STATE_ON_1:
            action->sled->ON1();
        break;
        case SLED_SCENE_SWITCH_IO_STATE_ON_0:
            action->sled->ON0();
        break;
        default:
        break;
    }
}

inline static void pwm_action_handler(sled_scene_action_t* action)
{
    action->sled->ConfigurePWM(action->pwm_param.pwm_duty);
    action->sled->SetMode(SLED_MODE_PWM);
}

inline static void breath_action_handler(sled_scene_action_t* action)
{
    action->sled->SetMode(SLED_MODE_BREATHE);
    action->sled->ConfigureBreathe(action->breathe_param.breath_speed);
    action->sled->ONBreathe();
}

uint32_t sled_scene_action_handler(sled_scene_action_t *action)
{
    // log_info("mode:%d\n",action->sled_mode);
    // logln_pos();
    switch (action->sled_mode)
    {
    case SLED_MODE_SWITCH:switch_action_handler(action);
        /* code */
        break;
    case SLED_MODE_PWM:pwm_action_handler(action);
        break;
    case SLED_MODE_BREATHE:breath_action_handler(action);
        break;
    default:
        break;
    }
    // log_info("breathe:%d\n",action->breathe_param.breath_speed);
    return action->keep_time_ms;
}

uint32_t sled_scene_handler(sled_scene_t *scene) 
{
    uint32_t keep;
    // logln_pos("index:%d,time:%d",scene->cur_action_index,(scene->actions_array[scene->cur_action_index].keep_time_ms));
    while(1)
    {
        sled_scene_action_handler(&(scene->actions_array[scene->cur_action_index]));
        keep = scene->actions_array[(scene->cur_action_index)].keep_time_ms;
        scene->cur_action_index ++;
        if(scene->actions_array[scene->cur_action_index].sled == NULL)
        {
            scene->cur_action_index = 0;
            if(scene->is_loop != true)
            {
                return 0;
            }
        }
        if(keep == 0)
            continue;
        else
            break;
        // scene->cur_action_index ++;
        // if(scene->actions_array[scene->cur_action_index].sled == NULL)
        // {
        //     scene->cur_action_index = 0;
        //     if(scene->is_loop != true)
        //     {
        //         return 0;
        //     }
        // }
    }
    return keep;
}